package com.team9.fitness.mapper;

import com.team9.fitness.entity.DietRecord;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 饮食记录数据访问层接口
 */
@Mapper
public interface DietRecordMapper {

    /**
     * 插入饮食记录
     */
    @Insert("INSERT INTO diet_record (user_id, food_id, food_name, amount, total_calories, total_carbohydrates, " +
            "total_protein, total_fat, meal_time, meal_type, create_time) " +
            "VALUES (#{userId}, #{foodId}, #{foodName}, #{amount}, #{totalCalories}, #{totalCarbohydrates}, " +
            "#{totalProtein}, #{totalFat}, #{mealTime}, #{mealType}, #{createdAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(DietRecord dietRecord);

    /**
     * 根据用户ID和日期查询饮食记录
     */
    @Select("SELECT * FROM diet_record WHERE user_id = #{userId} AND DATE(meal_time) = #{date} ORDER BY meal_time DESC")
    List<DietRecord> selectByUserIdAndDate(@Param("userId") Long userId, @Param("date") LocalDate date);

    /**
     * 根据用户ID查询所有饮食记录
     */
    @Select("SELECT * FROM diet_record WHERE user_id = #{userId} ORDER BY meal_time DESC")
    List<DietRecord> selectByUserId(Long userId);

    /**
     * 根据ID删除饮食记录
     */
    @Delete("DELETE FROM diet_record WHERE id = #{id}")
    int deleteById(Long id);
}
